import { DbConfig } from "../../src/config/DbConfig";
import { GameConfig } from "../../src/config/GameConfig";
import { DaoBase } from "../../src/engine/dao/DaoBase";
import DateUtil from "../../src/engine/utils/DateUtil";
import { Logger } from "../../src/engine/utils/Logger";
import { MysqlClient } from "../../src/engine/utils/MySqlClient";
import { MysqlManager } from "../../src/engine/utils/MysqlManager";

export class MatchUserDao extends DaoBase {

    public static DB_NAME: string = DbConfig.Db_Name.db_admin;
    public static Tb_Name: string = "t_match_users";

    public static async is_math_user_exist(userid: number, gameType: number): Promise<any> {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "select userid from " + this.DB_NAME + "." + this.Tb_Name + " where `userid`=? and `gameType`=?"
        let sqlParams = [userid, gameType];
        let results = await client.queryPromise(sql, sqlParams);
        if (results && results.length > 0) {
            return true
        }
        else {
            return false;
        }
    }

    public static async create_match_user(userid: number, gameType: number): Promise<any> {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = 'insert  into ' + this.Tb_Name + '(userid, last_time, gameType, turn,score,max_score) VALUES(?,?,?,?,?,?) ;';
        let timeStr: string = DateUtil.formatDateStr(DateUtil.now())
        let sqlParams = [userid, timeStr, gameType, 0, 0, 0];
        let rows = await client.queryPromise(sql, sqlParams);
        if (rows && rows["affectedRows"] > 0) {
            return true
        } else {
            return false
        }
    }

    public static async get_match_user(userid: number, gameType: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "select * from t_match_users WHERE userid = ? and gameType=?";
        let sqlParams = [userid, gameType];
        let results = await client.queryPromise(sql, sqlParams);
        if (results && results.length) {
            return results[0]
        }
    }

    public static async update_match_enroll(userid: number, gameType: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = 'UPDATE t_match_users SET last_time = ? WHERE userid = ? and gameType=?';
        let timeStr: string = DateUtil.formatDateStr(DateUtil.now())
        let sqlParams = [timeStr, userid, gameType];
        let results = await client.queryPromise(sql, sqlParams);
        if (results && results["affectedRows"] > 0) {
            return true
        }
        else {
            return false
        }
    }

    public static async update_match_turn(userid: number, gameType: number, turn: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = 'UPDATE t_match_users SET turn = ? WHERE userid = ? and gameType=?';
        let sqlParams = [turn, userid, gameType];
        let results = await client.queryPromise(sql, sqlParams);
        if (results && results["affectedRows"] > 0) {
            return true
        }
        else {
            return false
        }
    }

    public static async endMatchGame(userid: number, gameType: number, score: number, max_score: number, matchTurn: number) {
        if (matchTurn >= GameConfig.Max_Match_Turn) {
            score = 0
            matchTurn = 0
        }
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = 'UPDATE t_match_users SET score = ?, `max_score`=?, `turn`=? where `userid`=? and `gameType`=?';
        let sqlParams = [score, max_score, matchTurn, userid, gameType];
        let results = await client.queryPromise(sql, sqlParams);
        Logger.log("endMatchGame===", sql, sqlParams, results)
        if (results && results["affectedRows"] > 0) {
            return true
        }
        else {
            return false
        }
    }

    public static async clearMatchDaily(now: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = 'UPDATE t_match_users SET score = 0, `max_score`=0, `turn`=0 ';
        let results = await client.queryPromise(sql);
        if (results && results["affectedRows"] > 0) {
            return true
        }
        else {
            return false
        }
    }

    public static async getTop(gameType: number, topNum: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let startIndex: number = 0
        let sql: string = "select t_match_users.userid,t_match_users.max_score, t_users.nick_name,t_users.headimg, t_users.lv  from t_match_users left join t_users on t_match_users.userid=t_users.userid   where t_match_users.gameType=? order by max_score desc limit ?,?"
        let sqlParams = [gameType, startIndex, topNum];
        let rows = await client.queryPromise(sql, sqlParams);
        Logger.log("getTop=", rows)
        if (rows && rows.length) {
            return rows
        }
        return []
    }

    public static async getMyTop(gameType: number, userid: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "select t_match_users.userid,t_match_users.max_score, t_users.nick_name,t_users.headimg, t_users.lv from t_match_users left join t_users on t_match_users.userid=t_users.userid    where t_match_users.gameType=? and t_match_users.userid=?"
        let sqlParams = [gameType, userid];
        let rows = await client.queryPromise(sql, sqlParams);
        if (rows && rows.length) {
            return rows[0]
        }
    }
}